package heathot.servlet;

import heathot.bookservice.BookService;
import heathot.bookservice.BookServiceimpl;
import heathot.entiy.Car;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class BuyServlet extends HttpServlet {


	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
			request.setCharacterEncoding("utf-8");
			response.setCharacterEncoding("utf-8");
			response.setContentType("text/html,charset=utf-8");
			String id = request.getParameter("id");
			String currentPage = request.getParameter("currentPage");
			int i = 0;
			//System.out.println(id);
			if(id!=null&&!"".equals(id)){
				i= Integer.parseInt(id);
			}
			HttpSession session = request.getSession();
			Car car = (Car)session.getAttribute("car");
			if(car==null){
				car= new Car();
				session.setAttribute("car",car);
			}
			BookService service = new BookServiceimpl();
			service.addToCar(i,car);
			request.getRequestDispatcher("/servlet/PageServlet?pageCurrent="+currentPage).forward(request, response);
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
			doGet(request,response);
		
	}

}
